<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目: " prop="name">
        {{ this.queryParams.projectName }}
      </el-form-item>
      <!-- 搜索栏 -->
      <el-form-item label="路线名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入路线名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table v-loading="loading" :data="routeList">
      <el-table-column width="55" align="center" label="序号" type='index'>
        <template slot-scope="{$index}">
          <span style="margin-left: 10px">{{ (queryParams.pageNum - 1) * queryParams.pageSize + ($index + 1) }}</span>
        </template>
      </el-table-column>
      <!-- 五项满意度 -->
      <el-table-column label="路线名" align="center" prop="name" />
      <el-table-column label="总体满意度" align="center" prop="satisfactionResult[0]">
        <template slot-scope="scope">
          <span v-if="isNaN(scope.row.satisfactionResult[0])">暂无</span>
          <span v-else>{{ scope.row.satisfactionResult[0].toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="景点满意度" align="center" prop="satisfactionResult[1]">
        <template slot-scope="scope">
          <span v-if="isNaN(scope.row.satisfactionResult[1])">暂无</span>
          <span v-else>{{ scope.row.satisfactionResult[1].toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格和价值满意度" align="center" prop="satisfactionResult[1]">
        <template slot-scope="scope">
          <span v-if="isNaN(scope.row.satisfactionResult[2])">暂无</span>
          <span v-else>{{ scope.row.satisfactionResult[2].toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安全性满意度" align="center" prop="satisfactionResult[1]">
        <template slot-scope="scope">
          <span v-if="isNaN(scope.row.satisfactionResult[3])">暂无</span>
          <span v-else>{{ scope.row.satisfactionResult[3].toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动和娱乐满意度" align="center" prop="satisfactionResult[1]">
        <template slot-scope="scope">
          <span v-if="isNaN(scope.row.satisfactionResult[4])">暂无</span>
          <span v-else>{{ scope.row.satisfactionResult[4].toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看意见和建议</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 查看意见和建议表单 -->
    <el-dialog :title="title2" :visible.sync="open" width="600px" append-to-body>
      <el-form label-width="80px" v-if="type === 0">
        <el-form-item label="项目名:" prop="projectId">
          {{ this.queryParams.projectName }}
        </el-form-item>
        <el-form-item label="路线名:">
          {{ this.routeName }}
        </el-form-item>
        <!-- 意见和建议表单 -->
        <el-table :data="tableData" max-height="400" style="width: 100%">
          <el-table-column type="index" index="1">
          </el-table-column>
          <el-table-column prop="address" label="意见-建议">
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="type === 0">
        <el-button :loading="buttonLoading" type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRouteGrade, getRouteContent, satisfactionResult } from "@/api/lxy/route";
export default {
  name: "Route",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 路线-满意度评分
      routeList: [],
      // 弹出层标题
      title2: "查看意见和建议",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      //意见-建议集合
      tableData: [{
        address: '暂无评论1'
      }, {
        address: '暂无评论2'
      }, {
        address: '暂无评论3'
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询路线列表-满意度评分和建议 */
    getList() {
      this.queryParams.projectName = this.$route.query.projectName;
      this.queryParams.projectId = this.$route.query.projectId;
      this.loading = true;
      // 调用listRouteGrade方法，获取路线列表
      listRouteGrade(this.queryParams).then(response => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //根据路线id获取路线VO的List
    handlerouteContent(row) {
      this.loading = true;
      this.tableData = getRouteContent(row.id).map(tSelectUserVo => {
        return {
          address: tSelectUserVo.comment || '暂无评论'
        }
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        projectId: undefined,
        name: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看意见和建议按钮操作 */
    handleView(row) {
      this.loading = true;
      this.reset();
      this.type = 0;
      const id = row.id;
      satisfactionResult(id).then(response => {
        this.loading = false;
        // 清空原有的 tableData 数组
        this.tableData = [];
        // 处理获取的评论数据
        response.data.comments.forEach(comment => {
          this.tableData.push({ address: comment });
        });
        this.open = true;
        this.routeName = row.name;
      });
    },
  }
};
</script>
